function getData<T>(data:T,status?:boolean):T{
    return data
}

getData<string>('hello',true);

getData<number>(123);

//查找数组内的最小值 [2,3,4] ['a','b','c']

function getMin<T>(arr:T[]):T{  //给泛型默认值 T=number
    let min = arr[0];

    for(let i = 0;i<arr.length;i++){
        if(min > arr[i]){
            min = arr[i]
        }
    }
    return min 
}

console.log(getMin<number>([2,3,4]));

console.log(getMin<string>(['a','b','c']));


//根据参数创建长度length，元素为val的数组

function createArray<T>(length:number,val:T):T[]{
    let arr = new Array();

    for(let i = 0;i<length;i++){
        arr.push(val)
    }
    return arr
}

console.log(createArray<string>(3,'a')) //['a','a','a']

//数组交换两个值

function swp<T,U>(tuple:[T,U]):[U,T]{
    return [tuple[1],tuple[0]]
}


swp<number,string>([1,'a'])


//泛型类

class MinCls<T>{
    list:T[];
    constructor(){
        this.list = [];
    }
    add(num:T){
        this.list.push(num)
    }
    getMin():T{
        let min = this.list[0];

        for(let i = 0;i<this.list.length;i++){
            if(min > this.list[i]){
                min = this.list[i]
            }
        }
        return min 
    }
}

let min = new MinCls<string>();

min.add('a');

min.add('c');

console.log(min.getMin());
